package com.gency.subscribe.web.controller.system;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.baomidou.mybatisplus.plugins.Page;
import com.gency.subscribe.core.util.system.OSSClientUtils;
import com.gency.subscribe.model.system.PageBean;
import com.gency.subscribe.model.system.ServerBase;
import com.gency.subscribe.service.system.IServerBaseService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马行空
 * @since 2018-06-18
 */
@Controller
@RequestMapping("/server_base")
public class ServerBaseController {
	
	
	@Autowired
	IServerBaseService serverBaseService;
	
	
	@GetMapping("/list")
	public ModelAndView list(PageBean pb,ModelAndView mv){
		Page<ServerBase> page = new Page<ServerBase>(pb.getCurPage(),pb.getPageSize());
		page.setAsc(false);
		page.setOrderByField("create_time");
		page = serverBaseService.selectPage(page);
		List<ServerBase> list = page.getRecords();
		pb = new PageBean(page.getTotal(), page.getPages(), page.getCurrent(), 10, page.getOffsetCurrent(), page.getOffset(),0);
		mv.addObject("pagebean",pb);
		mv.addObject("serverlist", list);
		mv.setViewName("server/list");
		return mv;
	}
	
	
	@GetMapping("/add")
	public ModelAndView add(ModelAndView mv){
		mv.setViewName("server/info");
		mv.addObject("serverBase", new ServerBase());
		return mv;
	}
	
	
	@PostMapping("/add")
	public ModelAndView add(ModelAndView mv,ServerBase serverBase,MultipartFile multipartFile){
		String url ="";
		try {
			if(multipartFile != null && !multipartFile.isEmpty()){
				url = OSSClientUtils.URL_PREFIX+OSSClientUtils.putOSSObject(multipartFile);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		serverBase.setImage_url(url);
		String labs = "";
		if(serverBase.getLabe1()!=null && !serverBase.getLabe1().equals("")){
			labs = labs+serverBase.getLabe1()+",";
		}
		if(serverBase.getLabe2()!=null && !serverBase.getLabe2().equals("")){
			labs = labs+serverBase.getLabe2()+",";
		}
		if(serverBase.getLabe3()!=null && !serverBase.getLabe3().equals("")){
			labs = labs+serverBase.getLabe3()+",";
		}
		if(serverBase.getLabe4()!=null && !serverBase.getLabe4().equals("")){
			labs = labs+serverBase.getLabe4()+",";
		}
		if(serverBase.getLabe5()!=null && !serverBase.getLabe5().equals("")){
			labs = labs+serverBase.getLabe5()+",";
		}
		if(serverBase.getLabe6()!=null && !serverBase.getLabe6().equals("")){
			labs = labs+serverBase.getLabe6();
		}
		serverBase.setServer_tag(labs);
		mv.setViewName("redirect:list");
		serverBaseService.insert(serverBase);
		return mv;
	}
	
	
	@GetMapping("/update")
	public ModelAndView update(ModelAndView mv,Long id){
		mv.setViewName("server/info");
		ServerBase base = serverBaseService.selectById(id);
		try {
			if(base.getServer_tag()!=null && !base.getServer_tag().equals("")){
				base.setLabe1(base.getServer_tag().split(",")[0]);
				base.setLabe2(base.getServer_tag().split(",")[1]);
				base.setLabe3(base.getServer_tag().split(",")[2]);
				base.setLabe4(base.getServer_tag().split(",")[3]);
				base.setLabe5(base.getServer_tag().split(",")[4]);
				base.setLabe6(base.getServer_tag().split(",")[5]);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		mv.addObject("serverBase",base);
		return mv;
	}
	
	
	@PostMapping("/update")
	public ModelAndView update(ModelAndView mv,ServerBase serverBase,MultipartFile multipartFile){
		mv.setViewName("redirect:list");
		String url ="";
		try {
			if(multipartFile != null && !multipartFile.isEmpty()){
				url = OSSClientUtils.URL_PREFIX+OSSClientUtils.putOSSObject(multipartFile);
			}
		} catch (Exception e) {
			
		}
		
		if(!url.equals("")){
			serverBase.setImage_url(url);
		}
		String labs = "";
		if(serverBase.getLabe1()!=null && !serverBase.getLabe1().equals("")){
			labs = labs+serverBase.getLabe1()+",";
		}
		if(serverBase.getLabe2()!=null && !serverBase.getLabe2().equals("")){
			labs = labs+serverBase.getLabe2()+",";
		}
		if(serverBase.getLabe3()!=null && !serverBase.getLabe3().equals("")){
			labs = labs+serverBase.getLabe3()+",";
		}
		if(serverBase.getLabe4()!=null && !serverBase.getLabe4().equals("")){
			labs = labs+serverBase.getLabe4()+",";
		}
		if(serverBase.getLabe5()!=null && !serverBase.getLabe5().equals("")){
			labs = labs+serverBase.getLabe5()+",";
		}
		if(serverBase.getLabe6()!=null && !serverBase.getLabe6().equals("")){
			labs = labs+serverBase.getLabe6();
		}
		serverBase.setServer_tag(labs);
		serverBaseService.updateById(serverBase);
		return mv;
	}
	
	@GetMapping("/delete")
	public ModelAndView delete(ModelAndView mv,Long id){
		mv.setViewName("redirect:list");
		serverBaseService.deleteById(id);
		return mv;
	}
	
	@GetMapping("/updateSort")
	public ModelAndView updateSort(ModelAndView mv,Long id,Integer sort){
		mv.setViewName("redirect:list");
		if(id == null || sort == null){
			return mv;
		}
		ServerBase policyBase = new ServerBase();
		policyBase.setId(id);
		policyBase.setSort(sort);
		serverBaseService.updateById(policyBase);
		return mv;
	}
	
	
	@GetMapping("/sdopOrOpen")
	public ModelAndView sdopOrOpen(ModelAndView mv,Long id,Integer type){
		mv.setViewName("redirect:list");
		if(id == null || type == null){
			return mv;
		}
		ServerBase policyBase = new ServerBase();
		policyBase.setId(id);
		policyBase.setFlag_open(type);
		serverBaseService.updateById(policyBase);
		return mv;
	}
}

